<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-19 15:35
 *
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Illuminate\Support\Facades\DB;

class getCommentListCriteria extends BaseCriterias
{
	private $storeId;
	private $barberId;
	private $orderId;
	private $memberId;
	public function __construct($storeId,$barberId,$orderId=null,$memberId)
	{
		$this->storeId = $storeId;
		$this->barberId = $barberId;
		$this->orderId = $orderId;
		$this->memberId = $memberId;
	}

	public function apply($model, RepositoryInterface $repository)
	{

		$where = [
			'order_comment.user_id' => $this->barberId,
			'order_comment.store_id' => $this->storeId,
		];
		
		if(!empty($this->orderId)){
			$where['order_comment.order_id'] = $this->orderId;
		}

		$memberId = $this->memberId;

		return $model
			->select(['id','comment','view_times','order_id','member_id','created_at'])
			->with('member:id,name,face','reply:comment_id,contents,created_at')
			->with(['order'=>function($v){
				$v->select(['id','order_no','real_price','finish_time'])->with('items:id,order_id,service_name,count');
			}])
			->with(['orderCommentLike'=>function($v) use($memberId){
				$v->select(['comment_id','member_id'])->where('member_id','=',$memberId);
			}])
			->with(['resource'=>function($query){
				$query->select(['id','comment_id','resource_id','type']);
			}])
			->withCount('orderCommentLike')
			->where($where)
			;
	}
}